/* eslint-disable */
import pathToRegexp from 'path-to-regexp'
import http from '../request'
import * as api from '../api'

export const qupload = (file, prefix = '') => {
  const formData = new FormData();
  formData.append('file', file)
  return http.post(api.UPLOAD + '/productPic', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  }).then(rs => {
    if (rs.errno == 0) {
      return { key: rs.data.fileUrl, name: rs.data.name };
    }
    return
  });
}
export const getBaseUrl = (url) => {
  var reg = /^((\w+):\/\/([^/:]*)(?::(\d+))?)(.*)/
  reg.exec(url)
  return RegExp.$1
}
export const imgUrl = path => {
  // return `${process.env.WEBSITE}/storage/${path}`
  return `/storage/${path}`
}
export const keyMirror = (obj) => {
  let key
  let mirrored = {}
  if (obj && typeof obj === 'object') {
    for (key in obj) {
      if ({}.hasOwnProperty.call(obj, key)) {
        mirrored[key] = key
      }
    }
  }
  return mirrored
}
/**
 * 数组格式转树状结构
 * @param   {array}     array
 * @param   {String}    id
 * @param   {String}    pid
 * @param   {String}    children
 * @return  {Array}
 */
export const arrayToTree = (array, id = 'id', pid = 'pid', children = 'children') => {
  let data = array.map(item => ({ ...item }))
  let result = []
  let hash = {}
  data.forEach((item, index) => {
    hash[data[index][id]] = data[index]
  })

  data.forEach((item) => {
    let hashVP = hash[item[pid]]
    if (hashVP) {
      !hashVP[children] && (hashVP[children] = [])
      hashVP[children].push(item)
    } else {
      result.push(item)
    }
  })
  return result
}
export function getCurrentMenu(location, arrayMenu) {
  if (arrayMenu) {
    let current = []
    for (let i = 0; i < arrayMenu.length; i++) {
      const e = arrayMenu[i]
      const child = getCurrentMenu(location, e.children)
      if (child && child.length > 0) {
        child.push({ ...e, children: null })
        return child
      }
      if (e.href && pathToRegexp(e.href).exec(location)) {
        current.push({ ...e, children: null })
        return current
      }
    }
    return current
  }
  return null
}
export function randomStr() {
  return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
}
